<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/5/9
 * Time: 下午3:24
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Services\UserFilterServices;

class UserVerify extends Base
{
    protected $table = 'user_verify';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    public function add($userId, $token, $bizNo, $idcardName, $idcardNumber){

        $data = [
            'user_id' => $userId,
            'token' => $token,
            'biz_no' => $bizNo,
            'idcard_name' => $idcardName,
            'idcard_number' => $idcardNumber,
            'create_timestamp' => time()
        ];
        DB::table('user_verify')->insert($data);
    }

    public function setResult($token, $resultCode, $resultMessage){
        $record = $this->getInfoByUser(['token' => $token]);
        if (!$record) {
            return;
        }

        if ($this->getSuccess($record['user_id'])) {//已经有成功记录，直接返回
            return;
        }

        DB::table('user_verify')
            ->where('token', '=', $token)
            ->limit(1)
            ->update([
                'result_code' => $resultCode,
                'result_message' => $resultMessage,
                'result_timestamp' => time()
            ]);
    }

    public function getCount($userId){
        $count = DB::table('user_verify')
            ->where('user_id', '=', $userId)
            ->where('result_code', '<>', '')
            ->where('is_clear' , 0)
            ->count();
        return $count;
    }

    public function get($userId, $token){
        $row = DB::table('user_verify')
            ->where('token', '=', $token)
            ->where('user_id', '=', $userId)
            ->first();
        return $row;
    }


    public function getSuccess($userId){
        $row = DB::table('user_verify')
            ->where('result_code', '=', '1000')
            ->where('user_id', '=', $userId)
            ->first();
        if (empty($row)){
            return null;
        }
        return ['idcard_name'=>$row->idcard_name, 'idcard_number'=>$row->idcard_number];
    }

    public function getInfoByUser($params){
        $row = DB::table('user_verify')->where($params)->first();
        $row = self::objToArray($row);
        return $row;
    }


    public function getIdcardCount($idcardNumber){
        $count = DB::table('user_verify')
            ->where('result_code', '=', '1000')
            ->where('idcard_number', '=', $idcardNumber)
            ->count();
        return $count;
    }

    //获取用户实名认证账号
    public function getAllIcCard($idcardNumber){
        $row = DB::table('user_verify')
            ->where('result_code', '=', '1000')
            ->where('idcard_number', '=', $idcardNumber)
            ->get();
        $row = self::objToArray($row);
        return $row;
    }


    /**
     * 判断是否成年
     * @param $userId
     * @return -1.未找到\0.未成年\1.成年
     */
    public function isAdult($userId){
        $data = $this->getSuccess($userId);
        if (empty($data)){
            return -1;
        }

        if (strlen($data['idcard_number']) != 18) {// 非大陆
            return 1;
        }

        $age = getAgeByIdcard($data['idcard_number']);
        if ($age < 18){
            return 0;
        }
        return 1;
    }

    /**
     * 判断是否成年
     * @param $userId
     * @return -1.未找到\0.未成年\1.成年
     */
    public function isMan($idcardNumber){
        if (strlen($idcardNumber) != 18) {// 非大陆
            return 1;
        }

        $age = getAgeByIdcard($idcardNumber);
        if ($age < 18){
            return 0;
        }
        return 1;
    }

    /**
     * @desc 判断用户是否是在消费禁止名单
     * @param $userId
     * @return bool
     * @date 2022-02-24
     */
    public function isForbiddenConsumer($userId){
        return (new UserFilterServices)->inConsumeUserBlacklist($userId);
    }

    public function cmsAdd($userId, $token, $bizNo, $idcardName, $idcardNumber,$type = 1){
        if ($this->getSuccess($userId)) {//已经有成功记录，直接返回
            return true;
        }

        $data = [
            'user_id' => $userId,
            'token' => $token,
            'biz_no' => $bizNo,
            'result_code' => 1000,
            'result_message' => 'SUCCESS',
            'idcard_name' => $idcardName,
            'idcard_number' => $idcardNumber,
            'create_timestamp' => time(),
            'result_timestamp' => time(),
            'u_type' =>$type
        ];
        $res = DB::table('user_verify')->insert($data);
        if(!$res){
            return false;
        }
        return true;
    }

    /**注销-清认证数据
     * @param $user_id
     * @param $resultCode
     * @param $resultMessage
     */
    public function setResultEmpty($user_id, $resultCode, $resultMessage){
        DB::table('user_verify')
            ->where('user_id', '=', $user_id)
            ->update([
                'result_code' => $resultCode,
                'result_message' => $resultMessage,
                'result_timestamp' => time()
            ]);
    }

    /**
     * 判断是否满16周岁
     * @param $userId
     * @return -1.未找到\0.未满\1.满16
     */
    public function isOverSixteen($userId){
        $data = $this->getSuccess($userId);
        if (empty($data)){
            return -1;
        }

        if (strlen($data['idcard_number']) != 18) {// 非大陆
            return 1;
        }

        $age = getAgeByIdcard($data['idcard_number']);
        if ($age < 16){
            return 0;
        }
        return 1;
    }

    /**
     * 判断是否满18周岁
     * @param $userId
     * @return -1.未找到\0.未满\1.满18
     */
    public function isOverEighteen($userId){
        $data = $this->getSuccess($userId);
        if (empty($data)){
            return -1;
        }

        if (strlen($data['idcard_number']) != 18) {// 非大陆
            return 1;
        }

        $age = getAgeByIdcard($data['idcard_number']);
        if ($age < 18){
            return 0;
        }
        return 1;
    }
    public function clearVerify($user_id){
        return DB::table('user_verify')->where('user_id',$user_id)->where('result_code','!=','1000')->update(['is_clear'=>1]);
    }

    public function getIdCardNosByUserIds($userIds){
        return self::query()
            ->whereIn('user_id', $userIds)
            ->where('result_code', 1000)
            ->get();
    }

    public function getUidsByIdCardNo($idcardno){
        $result = self::query()
            ->where('idcard_number', $idcardno)
            ->where('result_code', 1000)
            ->get();
        $result = self::objToArray($result);
        return array_column($result, 'user_id');
    }


    /**
     * 根据用户id删除
     */
    public function deleteByUserId($userId){
        $res = DB::table('user_verify')
            ->where('user_id', $userId)
            ->delete();

        return $res;
    }


}
